﻿/********************************************************************
	created:	2011/05/12
	created:	12:5:2011   21:07
	filename: 	D:\My Documents\Visual Studio 2010\Projects\IntranetMessager\ServiceLibrary\WorkingCenter.cs
	file path:	D:\My Documents\Visual Studio 2010\Projects\IntranetMessager\ServiceLibrary
	file base:	WorkingCenter
	file ext:	cs
	author:		
	
	purpose:	数据初步处理并分工，获取消息类型后进行多线程异步调用处理
*********************************************************************/
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;
using CommonLibrary;

namespace ServiceLibrary
{
    public partial class ServiceRepository : IServiceRepository
    {
        private readonly Dictionary<string, MessageDelegate> _messageHandleList =
            new Dictionary<string, MessageDelegate>();

        private readonly ReaderWriterLockSlim _rwLocker = new ReaderWriterLockSlim();

        #region IServiceRepository Members

        public void SendData(byte[] message)
        {
            var envelope = Functions.DeserializeFromBytes<Protocol.MessageEnvelope>(message);
            var userChannel =
                OperationContext.Current.GetCallbackChannel<IClientCallBack>();
            string userSessionId = OperationContext.Current.SessionId;
            MessageDelegate handler = null;
            _rwLocker.EnterReadLock();
            try
            {
                if (envelope.MessageType != null)
                {
                    if (envelope.MessageType.FullName != null)
                        _messageHandleList.TryGetValue(envelope.MessageType.FullName, out handler);
                }
                if (handler != null)
                    handler.BeginInvoke(message, userChannel, userSessionId, null, null);
            }
            catch (Exception ex)
            {
                Printf("Select Message Handler error", ex, envelope.MyUid);
            }
            finally
            {
                _rwLocker.ExitReadLock();
            }
        }

        #endregion

        private void MessageHandleListAdd(string fullName, MessageDelegate handler)
        {
            _rwLocker.EnterWriteLock();
            try
            {
                if (fullName != null)
                    if (!_messageHandleList.ContainsKey(fullName))
                        _messageHandleList.Add(fullName, handler);
            }
            catch (Exception ex)
            {
                Printf("Message Handle List add error", ex);
            }
            finally
            {
                _rwLocker.ExitWriteLock();
            }
        }

        private void MessageHandlerListInit()
        {
            MessageHandleListAdd(typeof (Protocol.SignIn).FullName,
                                 Processdures.SignInProcessdure);
            MessageHandleListAdd(typeof (Protocol.SignOut).FullName,
                                 Processdures.SignOutProcessdure);
            MessageHandleListAdd(typeof (Protocol.TalkingPackage).FullName,
                                 Processdures.TalkingProcessdure);
            MessageHandleListAdd(typeof (Protocol.RegNewUser).FullName,
                                 Processdures.RegNewUserProcessdure);
            MessageHandleListAdd(typeof (Protocol.BroadcastPackage).FullName,
                                 Processdures.BroadcastProcessdure);
            MessageHandleListAdd(typeof (Protocol.GetOnlineUsers).FullName,
                                 Processdures.QueryAllOnlineUserProcessdure);
            MessageHandleListAdd(typeof (Protocol.GetNickName).FullName,
                                 Processdures.QueryNameByUidProcessdure);
            MessageHandleListAdd(typeof (Protocol.GetUid).FullName,
                                 Processdures.QueryUidByNameProcessdure);
            MessageHandleListAdd(typeof (Protocol.DesKeyUpdate).FullName,
                                 Processdures.DesKeyUpdateProcessdure);
            MessageHandleListAdd(typeof (Protocol.DesKeyUpdateAck).FullName,
                                 Processdures.DesKeyUpdateAckProcessdure);
        }

        #region MessageDelegate

        private delegate void MessageDelegate(byte[] byteMessage, IClientCallBack channel, string sessionId);

        #endregion
    }
}